package com.heima.user.service.impl;

import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.user.service.ApUserLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApUserLikeServiceImpl implements ApUserLikeService {
    @Autowired
    private CacheService cacheService;
    @Override
    public ResponseResult userFollow(UserRelationDto dto) {
        //使用redis实现
        //检查参数
        if(dto.getOperation() == null || dto.getOperation() < 0 || dto.getOperation() > 1){
            return ResponseResult.errorResult(200,"参数错误");
        }
        if (dto.getOperation() == 0) {
            //关注
            //判断是否已经关注
            if (cacheService.exists( dto.getAuthorId() + "_" + dto.getArticleId())) {
                return ResponseResult.errorResult(200,"已经关注");
            }
            cacheService.append(dto.getArticleId().toString()+"_"+dto.getAuthorId().toString(), dto.getAuthorId().toString());
            return ResponseResult.okResult("关注成功");
        } else {
            //取消关注
            cacheService.delete(dto.getArticleId().toString()+"_"+dto.getAuthorId().toString());
            return ResponseResult.okResult("取消关注成功");
        }
//        //1.判断用户是否已经关注
//        String key = "user_follow_" + dto.getAuthorId();
//        Boolean isMember = cacheService.sIsMember(key, dto.getArticleId().toString());
//        if(isMember){
//            //已经关注
//            return ResponseResult.errorResult(10086,"已经关注");
//        }
//        //2.添加关注
//        cacheService.append(key,dto.getArticleId().toString());

    }


}
